package com.lnsf.bookstore.web.serachHistory.controller;


import com.lnsf.bookstore.common.annotation.IgnoreAuth;
import com.lnsf.bookstore.common.annotation.LoginUser;
import com.lnsf.bookstore.common.controller.ApiBaseAction;
import com.lnsf.bookstore.web.keyWord.service.ApiKeywordService;
import com.lnsf.bookstore.web.keyWord.vo.ApiKeyWord;
import com.lnsf.bookstore.web.serachHistory.service.ApiSearchHistoryService;
import com.lnsf.bookstore.web.serachHistory.vo.ApiSerachHistory;
import com.lnsf.bookstore.web.user.vo.ApiUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * API登录授权
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-03-23 15:31
 */
@RestController
@RequestMapping("/api/search")
public class ApiSearchController extends ApiBaseAction {
    @Autowired
    private ApiKeywordService apiKeywordService;
    @Autowired
    private ApiSearchHistoryService apiSearchHistoryService;

    /*
     * 　　index
     */
    @RequestMapping("index")
    public Object index(@LoginUser ApiUser loginUser) {
        Map<String, Object> resultObj = new HashMap();
        Map param = new HashMap();
        param.put("isDefault", 1);
        param.put("page", 1);
        param.put("limit", 1);
        param.put("sidx", "id");
        param.put("order", "asc");
        List<ApiKeyWord> keywordsEntityList = apiKeywordService.queryList(param);
        //取出输入框默认的关键词
        ApiKeyWord defaultKeyword = null != keywordsEntityList && keywordsEntityList.size() > 0 ? keywordsEntityList.get(0) : null;
        //取出热闹关键词
        param = new HashMap();
        param.put("fields", "distinct keyword,is_hot");
        param.put("page", 1);
        param.put("limit", 10);
        param.put("sidx", "id");
        param.put("order", "asc");
        List<Map> hotKeywordList = apiKeywordService.hotKeywordList(param);
        //搜索历史
        param = new HashMap();
        param.put("userId", loginUser.getUserId());
        param.put("fields", "distinct keyword");
        param.put("page", 1);
        param.put("limit", 10);
        param.put("sidx", "id");
        param.put("order", "asc");
        List<ApiSerachHistory> historyVoList = apiSearchHistoryService.queryList(param);
        String[] historyKeywordList = new String[historyVoList.size()];
        if (null != historyVoList) {
            int i = 0;
            for (ApiSerachHistory historyVo : historyVoList) {
                historyKeywordList[i] = historyVo.getKeyword();
                i++;
            }
        }
        //
        resultObj.put("defaultKeyword", defaultKeyword);
        resultObj.put("historyKeywordList", historyKeywordList);
        resultObj.put("hotKeywordList", hotKeywordList);
        return toResponsSuccess(resultObj);
    }

    /*
     * 　　helper
     */
    @IgnoreAuth
    @RequestMapping("helper")
    public Object helper(@LoginUser ApiUser loginUser, String keyword) {
        Map param = new HashMap();
        param.put("fields", "distinct keyword");
        param.put("keyword", keyword);
        param.put("limit", 10);
        param.put("offset", 0);
        List<ApiKeyWord> keywords = apiKeywordService.queryList(param);
        String[] keys = new String[keywords.size()];
        if (null != keywords) {
            int i = 0;
            for (ApiKeyWord keywordsVo : keywords) {
                keys[i] = keywordsVo.getKeyword();
                i++;
            }
        }
        return toResponsSuccess(keys);
    }

    /*
     * 　　clearhistory
     */
    @RequestMapping("clearhistory")
    public Object clearhistory(@LoginUser ApiUser loginUser) {
        apiSearchHistoryService.deleteByUserId(loginUser.getUserId());
        return toResponsSuccess("");
    }
}
